package com.yellowpage.bigdata.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.opensymphony.xwork2.ActionContext;
import com.yellowpage.white.common.Configuration;
import com.yellowpage.white.common.Constants;
import com.yellowpage.white.struts.BaseAction;
import com.yellowpage.white.struts.Module;
import com.yellowpage.white.struts.OperationType;
import com.yellowpage.white.util.Util4Java;
import com.yellowpage.bigdata.data.NspFatallogRequsetSum;
import com.yellowpage.bigdata.service.NspFatallogRequsetSumService;
import com.yellowpage.ca.data.User;
import com.yellowpage.ca.service.RegionService;

public class NspFatallogRequsetSumAction extends BaseAction {

	public String RESULT_QUERY = "query";
	public String RESULT_ADD = "add";
	public String RESULT_EDIT = "edit";

	private Configuration configuration;

	public void setConfiguration(Configuration configuration) {
		this.configuration = configuration;
	}

	private List<NspFatallogRequsetSum> nspFatallogRequsetSumList = new ArrayList<NspFatallogRequsetSum>();
	private List<Map> resultList = new ArrayList<Map>();

	private NspFatallogRequsetSum nspFatallogRequsetSum;
	private NspFatallogRequsetSumService nspFatallogRequsetSumService;

	private String selectedId;
	private String[] selectedIds;
	private String message;
	private RegionService regionService;
	private String regionName;
	private String query_dataFlag;
	private String query_logDate;
	private String query_createUser;
	private String query_createTime;
	private String query_note;
	private String query_fatallogRequsetSumId;
	private String query_requestType;
	private String query_requestCount;
	private String query_logDate_end;
	private Map requestTypeMap = new HashMap();
	private Map<String, String> regionMap;
	public Map<String, String> getRegionMap() {
		return regionMap;
	}

	public String getRegionName() {
		return regionName;
	}

	public void setRegionName(String regionName) {
		this.regionName = regionName;
	}

	public void setRegionMap(Map<String, String> regionMap) {
		this.regionMap = regionMap;
	}

	public RegionService getRegionService() {
		return regionService;
	}

	public void setRegionService(RegionService regionService) {
		this.regionService = regionService;
	}
    public String getRegionSql(){
    	  Map session = ActionContext.getContext().getSession();
    	  User user = (User) session.get("userObject");
    	  String function_code= "OMC_ABILITYCALL_LOG_COUNT_REQUEST_DAYCOUNT";
    	  String regionsql= regionService.getRegionclauseByUserFunction(user.getUserId(), function_code, "REGION_CODE");
    	 return regionsql;
    }
	public void prepare() {
		super.prepare();
		// 加载请求类型
		requestTypeMap = nspFatallogRequsetSumService.getrequestTypeMap();
		if (this.selectedId != null && !this.selectedId.equals("")) {
			this.nspFatallogRequsetSum = nspFatallogRequsetSumService
					.getNspFatallogRequsetSum(Long.parseLong(selectedId));
		}
	}

	public String queryByPage() {
		initPageRender();
		
		Map queryMap = new HashMap();
		if(!regionName.equals("")){
			regionMap = nspFatallogRequsetSumService.getRegionMap(getRegionSql());
			String[] val = regionName.split(";");
			String str = "(";
			for(String ss : val){
				for(String key : regionMap.keySet()){
					if(regionMap.get(key).contains(ss)){
						str += "'" + key + "',";
						break;
					}
				}
			}
			str= str.substring(0, str.length() - 1)+")";
			queryMap.put("regionCode", str);
		}else{
			queryMap.put("regionCode", regionName);
		}
		queryMap.put("logDate", query_logDate);
		queryMap.put("query_logDate_end", query_logDate_end);
		queryMap.put("query_dataFlag", query_dataFlag);
		queryMap.put("requestType", query_requestType);

		resultList = nspFatallogRequsetSumService
				.queryNspFatallogRequsetSumByPage(queryMap, pageRender);

		setPageRenderToRequest();

		return RESULT_QUERY;
	}
	public String queryChannel(){
		initPageRender();
		regionMap = nspFatallogRequsetSumService.getRegionMap(getRegionSql());
		setPageRenderToRequest();
		return "toRegion";
	}
	public String getRESULT_QUERY() {
		return RESULT_QUERY;
	}

	public void setRESULT_QUERY(String rESULT_QUERY) {
		RESULT_QUERY = rESULT_QUERY;
	}

	public Configuration getConfiguration() {
		return configuration;
	}

	public NspFatallogRequsetSumService getNspFatallogRequsetSumService() {
		return nspFatallogRequsetSumService;
	}

	public String toAdd() {
		return RESULT_ADD;
	}

	public String save() {

		nspFatallogRequsetSumService
				.saveNspFatallogRequsetSum(nspFatallogRequsetSum);

		message = "新增成功!";
		return RESULT_ADD;
	}

	public void validateSave() {
		if (nspFatallogRequsetSum.getRegionCode().length() == 0) {
			this.addActionError("** RegionCode 不能为空 !");
		}
		if (nspFatallogRequsetSum.getDataFlag().length() == 0) {
			this.addActionError("** DataFlag 不能为空 !");
		}
		if (nspFatallogRequsetSum.getLogDate().length() == 0) {
			this.addActionError("** LogDate 不能为空 !");
		}
		if (nspFatallogRequsetSum.getNote().length() == 0) {
			this.addActionError("** Note 不能为空 !");
		}
		if (nspFatallogRequsetSum.getRequestType().length() == 0) {
			this.addActionError("** RequestType 不能为空 !");
		}

		this.setValidatorResult("/bigdata/NspFatallogRequsetSumAdd.jsp");
	}

	public String toEdit() {
		Long id = new Long(selectedId);

		nspFatallogRequsetSum = nspFatallogRequsetSumService
				.getNspFatallogRequsetSum(id);

		return RESULT_EDIT;
	}

	public String update() {

		nspFatallogRequsetSumService
				.updateNspFatallogRequsetSum(nspFatallogRequsetSum);

		message = "修改成功!";

		return RESULT_EDIT;
	}

	public void validateUpdate() {
		if (nspFatallogRequsetSum.getRegionCode().length() == 0) {
			this.addActionError("** RegionCode 不能为空 !");
		}
		if (nspFatallogRequsetSum.getDataFlag().length() == 0) {
			this.addActionError("** DataFlag 不能为空 !");
		}
		if (nspFatallogRequsetSum.getLogDate().length() == 0) {
			this.addActionError("** LogDate 不能为空 !");
		}
		if (nspFatallogRequsetSum.getNote().length() == 0) {
			this.addActionError("** Note 不能为空 !");
		}
		if (nspFatallogRequsetSum.getRequestType().length() == 0) {
			this.addActionError("** RequestType 不能为空 !");
		}
		this.setValidatorResult("/bigdata/NspFatallogRequsetSumUpdate.jsp");
	}

	public String delete() {
		List ids = new ArrayList();

		for (String id : selectedIds) {
			ids.add(Long.valueOf(id));
		}

		try {
			this.nspFatallogRequsetSumService.delete(ids);
		} catch (Exception e) {
			System.out.println("异常....");
			this.addActionError("** 可能含有子记录约束冲�?删除失败 !");
		}

		return queryByPage();
	}

	/**
	 * getters and setters
	 * 
	 * @return
	 */
	public void setNspFatallogRequsetSum(
			NspFatallogRequsetSum nspFatallogRequsetSum) {
		this.nspFatallogRequsetSum = nspFatallogRequsetSum;
	}

	public NspFatallogRequsetSum getNspFatallogRequsetSum() {
		return nspFatallogRequsetSum;
	}

	public void setNspFatallogRequsetSumService(
			NspFatallogRequsetSumService nspFatallogRequsetSumService) {
		this.nspFatallogRequsetSumService = nspFatallogRequsetSumService;
	}

	public List<NspFatallogRequsetSum> getNspFatallogRequsetSumList() {
		return nspFatallogRequsetSumList;
	}

	public void setNspFatallogRequsetSumList(
			List<NspFatallogRequsetSum> nspFatallogRequsetSumList) {
		this.nspFatallogRequsetSumList = nspFatallogRequsetSumList;
	}

	public String getSelectedId() {
		return selectedId;
	}

	public void setSelectedId(String selectedId) {
		this.selectedId = selectedId;
	}

	public String[] getSelectedIds() {
		return selectedIds;
	}

	public void setSelectedIds(String[] selectedIds) {
		this.selectedIds = selectedIds;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public List<Map> getResultList() {
		return resultList;
	}

	public void setResultList(List<Map> resultList) {
		this.resultList = resultList;
	}


	public String getQuery_dataFlag() {
		return this.query_dataFlag;
	}

	public void setQuery_dataFlag(String dataFlag) {
		this.query_dataFlag = dataFlag;
	}

	public String getQuery_logDate() {
		return this.query_logDate;
	}

	public void setQuery_logDate(String logDate) {
		this.query_logDate = logDate;
	}

	public String getQuery_createUser() {
		return this.query_createUser;
	}

	public void setQuery_createUser(String createUser) {
		this.query_createUser = createUser;
	}

	public String getQuery_createTime() {
		return this.query_createTime;
	}

	public void setQuery_createTime(String createTime) {
		this.query_createTime = createTime;
	}

	public String getQuery_note() {
		return this.query_note;
	}

	public void setQuery_note(String note) {
		this.query_note = note;
	}

	public String getQuery_fatallogRequsetSumId() {
		return this.query_fatallogRequsetSumId;
	}

	public void setQuery_fatallogRequsetSumId(String fatallogRequsetSumId) {
		this.query_fatallogRequsetSumId = fatallogRequsetSumId;
	}

	public String getQuery_requestType() {
		return this.query_requestType;
	}

	public void setQuery_requestType(String requestType) {
		this.query_requestType = requestType;
	}

	public String getQuery_requestCount() {
		return this.query_requestCount;
	}

	public void setQuery_requestCount(String requestCount) {
		this.query_requestCount = requestCount;
	}

	public String getQuery_logDate_end() {
		return query_logDate_end;
	}

	public void setQuery_logDate_end(String query_logDate_end) {
		this.query_logDate_end = query_logDate_end;
	}

	public Map getRequestTypeMap() {
		return requestTypeMap;
	}

	public void setRequestTypeMap(Map requestTypeMap) {
		this.requestTypeMap = requestTypeMap;
	}

}
